clear
pr drop _all
set more off

***********************
*  Likelihood Evaluator                                                           
***********************

program define splag_ll

args lnf mu rho sigma
tempvar A rSL
gen `A'= ones - `rho'*EIGS1
gen `rSL'=`rho'*SL1
qui replace `lnf'= ln(`A') + ln(normden($ML_y1-`rSL'-`mu', 0, `sigma'))
end

global nobs = 19

***********************
* Open Data For Weights
***********************
clear
use "C:\Documents and Settings\jchays\Desktop\Courses\PS 531\Pols531_Documents\F_07\fake_weights.dta", clear
***********************
mkmat var1-var$nobs, matrix(W)
matrix eigenvalues eig1 imaginaryv = W
matrix eig2 = eig1'
matrix ones=J($nobs,1,1)


**************************
* Open Data for Regression
**************************
drop _all
use "C:\Documents and Settings\jchays\Desktop\Courses\PS 531\Pols531_Documents\F_07\fake_data.dta", clear
**************************
global Y survival
global X GDP MILITARY
mkmat $Y, matrix(Y)
matrix SL = W*Y
svmat SL, n(SL)
svmat eig2, n(EIGS)
svmat ones, n(ones)

*************************
*Produce starting values 
*************************
qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

***************************
*Estimate spatial lag model
***************************

	ml model lf splag_ll (mu: $Y=$X) (rho:) (sigma:)
	ml init OLSb
	ml init rho:_cons=0
	ml init sigma:_cons=`OLSsigma'
	*ml check
	*ml report
	ml max
	



*Alternative Likelihood: ln(`A') - .5*ln(2*_pi) - .5*ln((`sigma')^2) - ((.5/((`sigma')^2))*(($ML_y1-`rSL'-`mu')^2))

